<?php

use Biz\model\GzDesignWeekActivity\GzDesignWeekActivityModel;

class SaveAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    /**
     * @throws \Exception
     */
    public function main()
    {
        $currentUid = $this->uid;
        $taskId = trim($this->getRequest()->getPost('task_id', 0));
        \Doraemon\tools\Log::info('GzDesignWeekActivity/Save', compact('currentUid', 'taskId'));
        if ($taskId < 1 || $taskId > 4 || $currentUid == 0) {
            throw new Exception('参数错误');
        }

        $map = [
            'uid' => $currentUid,
            'task_id' => $taskId,
            'status' => GzDesignWeekActivityModel::STATUS_NORMAL,
        ];

        $count = GzDesignWeekActivityModel::countByWhere($map);
        if ($count == 0) {
            GzDesignWeekActivityModel::insertData($map);
        } else {
            throw new Exception("您已经获取过该经验值");
        }

        $this->setSuccess([], self::API_CODE_SUCCES);
    }
}
